/*
 * Credit.java
 *
 * This programs demonstrates the use of nested loop to
 * allow a user to purchase car-related items until the credit exists.
 *
 *
 
 */

package com.javacore.day3.gamecredit;

import java.util.Scanner;

/**
 *
 * @author vijayk
 */
public class Credit {
    
    /** Creates a new instance of Credit */
    public Credit() {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int credit = 5000, choice = 0;
        int itemPrice = 0;
        Scanner input = new Scanner(System.in);
        
        // Iterate until the user chooses to exit
        for(;(credit > 0) && (choice != 5); ) {
            
            // Iterate until the the user chooses to exit
            // or the credit is over
            do {
                
                // Display list of items to purchase
                // with an option to exit
                System.out.println("\n1. Engine - $3000");
                System.out.println("2. Suspension - $1500");
                System.out.println("3. Tyre - $500");
                System.out.println("4. Music System - $3000");
                System.out.println("5. Exit ");
                
                System.out.println("\nYour current credit is: $" + credit);
                System.out.println("\nEnter your choice? ");
                choice = input.nextInt();
                
                // if the user enters 5 as the choice, exit the loop
                if(choice == 5){
                    break;
                }
                
                // Determine the price of the item selected by the user
                switch(choice) {
                    
                    case 1:
                        itemPrice =  3000;
                        break;
                        
                    case 2:
                        itemPrice = 1500;
                        break;
                        
                    case 3:
                        itemPrice =500;
                        break;
                        
                    case 4:
                        itemPrice = 3000;
                        break;
                        
                    default:
                        System.out.println("Invalid choice\n");
                }
                
                // If the user has credits to purchase the item, update
                // the credits value, or exit the loop
                if (credit >= itemPrice)
                    credit -= itemPrice;
                else if (itemPrice > credit) {
                    System.out.println("Sorry, the item price(" + itemPrice +
                            ") exceeds your current credit(" + credit +  ")\n");
                    break;
                }
                
                
            } while(choice >= 1 && choice <= 4 );
            
        }
        
    }
    
}